import json

from django.db import models
from utils.model_base_class import ModelBase


class Customer(ModelBase):
    """
    客户信息
    """

    GRADE_CHOICES = (
        ("A", "重要客户"),
        ("B", "优质客户"),
        ("C", "普通客户"),
    )

    PLATFORM_CHOICES = (
        (1, "淘宝专卖店"),
        (2, "京东专卖店"),
        (3, "拼多多专卖店"),
    )

    id = models.BigAutoField(primary_key=True, verbose_name="编号")
    name = models.CharField(max_length=20, null=False, verbose_name="客户名")
    nickname = models.CharField(max_length=20, blank=True, null=True, verbose_name="称呼")

    telephone = models.CharField(max_length=11, null=True, blank=True, verbose_name="电话", help_text="手机号，字符串，可选")
    wechat = models.CharField(max_length=255, blank=True, null=True, verbose_name="微信号", help_text="微信号，字符串，可选")
    qq = models.IntegerField(blank=True, null=True, verbose_name="QQ", help_text="QQ，字符串，可选")
    address_code = models.CharField(max_length=255, blank=True, null=True, verbose_name="地址编码", help_text="地址编码列表，列表，可选")

    grade = models.CharField(max_length=20, default="C", choices=GRADE_CHOICES, verbose_name="等级", help_text="等级，默认为A(重要客户)")
    platform = models.IntegerField(default=1, choices=PLATFORM_CHOICES, verbose_name="平台", help_text="平台，默认为1(淘宝专卖店)")

    class Meta:
        db_table = 'module_customer'
        verbose_name = "客户信息"
        verbose_name_plural = "客户信息"

    @classmethod
    def format_request_data(cls, data):
        data["address_code"] = json.dumps(data["address_code"])
        return data

    def get_address_code(self):
        if not self.address_code: return ""
        return json.loads(self.address_code)

